#include <fstream>
#include "txtpara.h"
#include <cstdlib>
#include <cstring>

int getparaINT(string paraname, string filename)
{
    ifstream parafile;
    int result;
    char ch;
    char tmp[256];
    parafile.open(filename.c_str());
    while (!parafile.eof())
    {
        string cur_para;
        cur_para = "";
        parafile >> ch;
        if (ch == '#')
        {                        // skip comments
            parafile.getline(tmp, 256);
            parafile >> ch;
        }
        while (ch != '=')
        {
            cur_para += ch;
            parafile >> ch;
        }
        if (cur_para == paraname)
        {
            parafile >> result;
            break;
        }
        else
        {
            parafile.getline(tmp, 256);
        }
    }
    parafile.close();
    return result;
}

float getparaFLOAT(string paraname, string filename)
{
    ifstream parafile;
    float result;
    char ch;
    char tmp[256];
    parafile.open(filename.c_str());
    while (!parafile.eof())
    {
        string cur_para;
        cur_para = "";
        parafile >> ch;
        if (ch == '#')
        {                        // skip comments
            parafile.getline(tmp, 256);
            parafile >> ch;
        }
        while (ch != '=')
        {
            cur_para += ch;
            parafile >> ch;
        }
        if (cur_para == paraname)
        {
            parafile >> result;
            break;
        }
        else
        {
            parafile.getline(tmp, 256);
        }
    }
    parafile.close();
    return result;
}

string getparaSTRING(string paraname, string filename)
{
    ifstream parafile;
    string result;
    char ch;
    char tmp[256];
    parafile.open(filename.c_str());
    while (!parafile.eof())
    {
        string cur_para;
        cur_para = "";
        parafile >> ch;
        if (ch == '#')
        {                        // skip comments
            parafile.getline(tmp, 256);
            parafile >> ch;
        }
        while (ch != '=')
        {
            cur_para += ch;
            parafile >> ch;
        }
        if (cur_para == paraname)
        {
            parafile >> result;
            break;
        }
        else
        {
            parafile.getline(tmp, 256);
        }
    }
    parafile.close();
    return result;
}

void getparaFLOATARRAY(string paraname, string filename, float *floatpara,
                       int NR_elem)
{
    ifstream parafile;
    string result;
    char ch;
    char tmp[256];
    parafile.open(filename.c_str());
    while (!parafile.eof())
    {
        string cur_para;
        cur_para = "";
        parafile >> ch;
        if (ch == '#')
        {                        // skip comments
            parafile.getline(tmp, 256);
            parafile >> ch;
        }
        while (ch != '=')
        {
            cur_para += ch;
            parafile >> ch;
        }
        if (cur_para == paraname)
        {
            parafile >> result;
            break;
        }
        else
        {
            parafile.getline(tmp, 256);
        }
    }
    parafile.close();
    const char *sep = ",";
    char *str = new char[result.length()];
    strcpy(str, result.c_str());
    int i = 0;
    char *p = strtok(str, sep);
    while (p)
    {
        floatpara[i] = atof(p);
        p = strtok(NULL, sep);
        i++;
        if (i > NR_elem)
        {
            cout << "parameter " << paraname << " read error!" << endl;
            delete[]str;
            exit(0);
        }
    }
    delete[]str;
}

void getparaINTARRAY(string paraname, string filename, int *intpara,
                     int NR_elem)
{
    ifstream parafile;
    string result;
    char ch;
    char tmp[256];
    parafile.open(filename.c_str());
    while (!parafile.eof())
    {
        string cur_para;
        cur_para = "";
        parafile >> ch;
        if (ch == '#')
        {                        // skip comments
            parafile.getline(tmp, 256);
            parafile >> ch;
        }
        while (ch != '=')
        {
            cur_para += ch;
            parafile >> ch;
        }
        if (cur_para == paraname)
        {
            parafile >> result;
            break;
        }
        else
        {
            parafile.getline(tmp, 256);
        }
    }
    parafile.close();
    const char *sep = ",";
    char *str = new char[result.length()];
    strcpy(str, result.c_str());
    int i = 0;
    char *p = strtok(str, sep);
    while (p)
    {
        intpara[i] = atoi(p);
        p = strtok(NULL, sep);
        i++;
        if (i > NR_elem)
        {
            cout << "parameter " << paraname << " read error!" << endl;
            delete[]str;
            exit(0);
        }
    }
    delete[]str;
}
